package org.lebutch.sw;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import wonder.Board;
import wonder.IResource.ResourceType;
import wonder.ISWService;
import wonder.ISWService.BoardSet;
import wonder.SWServiceFactory;
import wonder.settings.SettingsManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.util.Log;

public class SevenWondersApp extends ToolsApp {
	private int playersCount = 0;
	private static final int DEFAULT_PLAYERS_COUNT = 3;

	@Override
	public void onCreate() {
		super.onCreate();

		try {
			SettingsManager.standalone();
		} catch (Exception e) {
			Log.e(Constants.LOG_TAG, "Exception initializing 7w-wonders-api", e);
		}
	}

	public String getApplicationVersion() {
		String rc = "n/a";
		try {
			rc = this.getPackageManager().getPackageInfo(this.getPackageName(),
					0).versionName;
		} catch (Exception e) {
			Log.v(Constants.LOG_TAG, e.getMessage());
		}
		return rc;
	}

	public int getCurrentPlayersCount() {
		if (playersCount == 0) {
			SharedPreferences prefs = PreferenceManager
					.getDefaultSharedPreferences(getBaseContext());
			playersCount = Integer.parseInt(prefs.getString(
					Constants.NUM_PLAYERS,
					String.valueOf(DEFAULT_PLAYERS_COUNT)));
		}

		return playersCount;
	}

	public void setCurrentPlayersCount(int selectedItemPosition) {
		playersCount = selectedItemPosition;
	}

	public List<BoardSet> getAvailableSets() {
		List<BoardSet> rc = new ArrayList<BoardSet>();
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(getBaseContext());

		try {
			ISWService manager = SWServiceFactory.getInstance().getSWService();
			List<BoardSet> wSets = manager.getSetList();
			for (BoardSet wSet : wSets) {
				boolean isAvailable = prefs.getBoolean(wSet.name(),
						BoardSet.BASE.equals(wSet));
				if (isAvailable) {
					rc.add(wSet);
					manager.setAvailability(wSet, true);
				}
			}
		} catch (Exception e) {
			Log.e(Constants.LOG_TAG,
					"Error configuring available sets of wonders", e);
		}

		Log.d(Constants.LOG_TAG, "getAvailableSets() " + rc);
		return rc;
	}

	public int getIdentifier(String key) {
		return getResources().getIdentifier(key, "string", "org.lebutch.sw");
	}

	public Drawable getResourceDrawable(Context context, ResourceType type) {
		Drawable drw = null;
		String fileName = "res_" + type.name().toLowerCase() + ".png";

		try {
			if (!Arrays.asList(context.getAssets().list("")).contains(fileName)) {
				fileName = "res_undefined.png";
			}
			drw = new BitmapDrawable(getResources(), context.getAssets().open(
					fileName));
		} catch (IOException e) {
			Log.w(Constants.LOG_TAG, "Exception getting image: " + fileName
					+ " " + e.toString());
		}
		return drw;
	}

	public Drawable getBoardStepsDrawable(Context context, Board board) {
		Drawable drw = null;
		String fileName = "steps_" + board.getName().toLowerCase() + "_"
				+ board.getSide().name().toLowerCase() + ".jpg";

		try {
			Log.d(Constants.LOG_TAG, "Looking for " + fileName);
			if (!Arrays.asList(context.getAssets().list("")).contains(fileName)) {
				fileName = "res_undefined.png";
			}
			drw = new BitmapDrawable(getResources(), context.getAssets().open(
					fileName));
		} catch (IOException e) {
			Log.w(Constants.LOG_TAG, "Exception getting image: " + fileName
					+ " " + e.toString());
		}
		return drw;
	}
}
